package com.joker.controller;

import com.alibaba.fastjson.JSON;
import com.joker.bean.Course;
import com.joker.service.CourseService;
import com.joker.service.StudentService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/getYourCourses/{id}")
    public String getOwnStudent(@PathVariable String id,HttpServletRequest request){
        List<Course> courses = courseService.getStudentCourse(id);
        String s = JSON.toJSONString(courses);
        s = s.replaceAll("\\[", "");
        s = s.replaceAll("]", "");
        System.out.println(courses);
        request.getSession().setAttribute("action","yourCourses");
        request.getSession().setAttribute("courses",s);
        return "../../pages/students/scourseList.jsp";
    }

    @RequestMapping(value = "/getOneCourse/{id}")
    public String getOneCourse(@PathVariable String id, HttpServletRequest request){
        List<Course> courses = courseService.getStudentCourse(id);
        System.out.println(courses);
        request.getSession().setAttribute("action","course");
        request.getSession().setAttribute("courses",courses);
        return "../../pages/students/scourseList.jsp";
    }

    @RequestMapping(value = "/getOutCourse/{id}")
    public String getOutCourses(@PathVariable String id, HttpServletRequest request){
        Map<String, Integer> maps = new HashedMap();
        List<Course> courses = courseService.getOutCourse(id);
        System.out.println(courses);
        List<Course> oneCourse = courseService.getStudentCourse(id);
        List<Course> rs = new ArrayList<>();
        System.out.println(oneCourse);
        for (Course course : courses) {
            boolean kill = false;
            for (Course one : oneCourse) {
                if(course.getWeek().equals(one.getWeek())){
                    if(Integer.parseInt(course.getBegin1()) >= Integer.parseInt(one.getBegin1()) || Integer.parseInt(course.getEnd1()) <= Integer.parseInt(one.getEnd1())){
                        kill = true;
                    }
                }
            }
            if(!kill){
                rs.add(course);
                kill = false;
            }
        }
        System.out.println("-----------");
        System.out.println(rs);
        System.out.println("-----------");
        request.getSession().setAttribute("action","selectCourses");
        request.getSession().setAttribute("courses",rs);
        return "../../pages/students/scourseList.jsp";
    }

    @RequestMapping(value = "/addStudentCourse/{cid}/{sid}")
    public String addStudentCourse(@PathVariable String cid,@PathVariable String sid){
        int rs = studentService.addOneCourse(cid, sid);
        if(rs>0){
            System.out.println("学生添加课程成功");
        }
        return "../../getOutCourse/"+sid;
    };
}
